package com.sunny.ynhz.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunny.ynhz.R;
import com.sunny.ynhz.bean.PatrolMoreStyleBean;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 多样化adapter
 * Created by luao on 2017/11/16.
 */

public class MoreStyleAdapter extends RecyclerView.Adapter<MoreStyleAdapter.ViewHolder> {
    private List<PatrolMoreStyleBean> mData;

    public MoreStyleAdapter(List<PatrolMoreStyleBean> data) {
        mData = data;

    }

    public interface OnItemClick {

        /**
         * 上传按钮被点击
         *
         * @param data
         */
        void onUpLoadClicked(PatrolMoreStyleBean data);

        /**
         * 删除按钮被点击
         *
         * @param data
         */
        void onDeleteClicked(PatrolMoreStyleBean data);

        /**
         * 界面被点击
         *
         * @param data
         */
        void onItemClick(PatrolMoreStyleBean data);
    }

    private OnItemClick onItemClick = null;

    public void setOnItemClick(OnItemClick onItemClick) {
        this.onItemClick = onItemClick;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_more_style, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        PatrolMoreStyleBean data = mData.get(position);
        holder.bindData(data);
        holder.setPositionOnClicked(position);

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private void bindData(PatrolMoreStyleBean bean) {
            setPatrolMoreStyleBean(bean);
            startTime.setText(bean.getTm());
            endTime.setText("文件类：" + bean.getFileNums() + "个  图片类：" + bean.getPicNums() + "张");
            if (patrolMoreStyleBean.getDescribe() != null) {
                distance.setText("履职描述："+patrolMoreStyleBean.getDescribe());
            } else {
                distance.setText("履职描述：");
            }
            uploadPatrol.setOnClickListener(this);
            deletePatrol.setOnClickListener(this);
            itemView.setOnClickListener(this);
        }


        public void setPatrolMoreStyleBean(PatrolMoreStyleBean bean) {
            this.patrolMoreStyleBean = bean;
        }

        public void setPositionOnClicked(int positionOnClicked) {
            this.positionOnClicked = positionOnClicked;
        }

        private PatrolMoreStyleBean patrolMoreStyleBean;
        private int positionOnClicked;

        @BindView(R.id.start_time)
        TextView startTime;
        @BindView(R.id.end_time)
        TextView endTime;
        @BindView(R.id.upload_patrol)
        TextView uploadPatrol;
        @BindView(R.id.delete_patrol)
        TextView deletePatrol;
        @BindView(R.id.distance)
        TextView distance;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void onClick(View view) {
            if (onItemClick == null) {
                return;
            }
            if (view.equals(uploadPatrol)) {
                onItemClick.onUpLoadClicked(patrolMoreStyleBean);
            } else if (view.equals(deletePatrol)) {
                onItemClick.onDeleteClicked(patrolMoreStyleBean);
            } else if (view.equals(itemView)) {
                onItemClick.onItemClick(patrolMoreStyleBean);
            }
        }
    }
}
